<?php
require_once __DIR__ . '/../init.php';
require_once __DIR__ . '/../../util/common.php';
date_default_timezone_set('GMT');
Resque::setBackend('127.0.0.1:6379');
$db=MysqliDB::getIntance('push');

$sql="select * from push";
$pushs=$db->getAll($sql);
$sql="select * from push_link";
$links=$db->getAll($sql);

foreach ($pushs as $push){
    $id = $push['id'];
    $apis[$id]['domain'] = $push['domain'];
    $apis[$id]['token'] = $push['token'];
}

foreach ($links as $link){
    $domain_id = $link['domain_id'];
    if ($link['type'] == '泛站子域名链接'){
        $site = str_ireplace(['http://','https://'],'',$link['link']);
        $token = $apis[$domain_id]['token'];
        $datas[$site]['main'] = $apis[$domain_id]['domain'];
        $datas[$site]['site'] = $site;
        $datas[$site]['api'] = "http://data.zz.baidu.com/urls?site=$site&token=$token";
        $datas[$site]['urls'] = GetSjzfc(rtrim($link['link'],'/').'/news/');
    }
}

//$i = 0;
//队列排好后，每次入队一个域名分给一个任务（测试）
foreach ($datas as $data){
    //测试，限制入队数量
    /*if ($i == 1){
        break;
    }*/
    $args = array(
        'time' => time(),
        'data' =>$data,
    );
    $jobIds[] = Resque::enqueue('push', 'push_job', $args, true);
    //$i++;
}
echo "Queued job ".count($jobIds)."个\n\n";

function GetSjzfc( $url = '', $length = 10 ,$num = 200)  {
    if(empty($url)) return '';
    $str = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
    str_shuffle($str);
    $data = array();
    for($i = 0; $i < $num; $i++){
        $name = substr(str_shuffle($str),0,$length);
        $data[] = $url.$name.'.xml';
    }
    return $data;
}
